//
//  seqlist.c
//  线性表顺序存储与实现
//
//  Created by ZJ on 2018/7/13.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#include "seqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 链表中的数据结构
typedef struct _tag_SeqList {
    int length;
    int capacity;
    unsigned int** node; // int* node[] 指针数组
} TSeqList;

SeqList* SeqList_Create(int capacity)
{
    TSeqList *temp = NULL;
    int ret = 0;
    
    temp = (TSeqList *)malloc(sizeof(TSeqList));
    if (temp == NULL) {
        ret = -1;
        printf("func SeqList_Create() err: %d\n", ret);
        return NULL;
    }
    
    memset(temp, 0, sizeof(TSeqList));
    
    // 根据capacity 的大小分配节点的空间
    temp->node = (unsigned int **)malloc(sizeof(unsigned int *) * capacity);
    if (temp->node == NULL) {
        ret = -2;
        printf("func SeqList_Create() err: malloc error %d\n", ret);
        return NULL;
    }
    
    temp->length = 0;
    temp->capacity = capacity;
    
    return temp;
}

void SeqList_Destroy(SeqList* list)
{
    TSeqList *tList = NULL;
    if (list == NULL) {
        return;
    }
    
    tList = (TSeqList *)list;
    if (tList->node != NULL) {
        free(tList->node);
        tList->node = NULL;
        tList->length = 0;
        tList->capacity = 0;
    }
    
    free(tList);
}

// 清空链表 //回到初始化状态
void SeqList_Clear(SeqList* list)
{
    TSeqList *tList = NULL;
    if (list == NULL) {
        return;
    }
    
    tList = (TSeqList *)list;
    tList->length = 0;
}

int SeqList_Length(SeqList* list)
{
    TSeqList *tList = NULL;
    if (list == NULL) {
        return -1;
    }
    
    tList = (TSeqList *)list;
    return tList->length;
}

int SeqList_Capacity(SeqList* list)
{
    TSeqList *tList = NULL;
    if (list == NULL) {
        return -1;
    }
    
    tList = (TSeqList *)list;
    return tList->capacity;
}

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
    int ret = 0;
    TSeqList *tList = NULL;
    if (list == NULL || node == NULL || pos < 0) {
        ret = -1;
        printf("func SeqList_Insert() error: %d", ret);
        return ret;
    }
    
    tList = (TSeqList *)list;
    
    // 判断是否满了
    if (tList->length >= tList->capacity) {
        ret = -2;
        printf("func SeqList_Insert() tList->length >= tList->capacity error: %d", ret);
        return ret;
    }

    // 容错修正
    if (pos > tList->length) {
        pos = tList->length;
    }
    
    // 1 元素后移
    for (int i = tList->length; i > pos; i--) {
        tList->node[i] = tList->node[i - 1];
    }
    
    // 2 插入元素
    tList->node[pos] = node;
    tList->length ++;
    
    return ret;
}

SeqListNode* SeqList_Get(SeqList* list, int pos)
{
    TSeqList *tList = NULL;
    if (list == NULL || pos < 0) {
        printf("func SeqList_Get() error.");
        return NULL;
    }
    
    tList = (TSeqList *)list;
    return tList->node[pos];
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
    TSeqList *tList = NULL;
    SeqListNode *ret = NULL;
    
    if (list == NULL || pos < 0) { // 检查
        printf("func SeqList_Delete() error.");
        return ret;
    }
    
    tList = (TSeqList *)list;
    ret = tList->node[pos]; // 缓存pos的值
    
    for (int i = pos + 1; i < tList->length; i++) { // pos位置后面的元素前移
        tList->node[i - 1] = tList->node[i];
    }
    
    tList->length --;
    
    return ret;
}
